package com.demo2do.fms.admin.web.controller.supplier.management;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.TeamSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.TeamService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.driver.Team;

/**
 * 车队信息管理
 *
 * @author Wy
 * @author zhonghui
 */
@Controller
@RequestMapping("/teams")
@MenuSetting("menu-supplier")
public class TeamController {
    
    private static final Logger logger = LoggerFactory.getLogger(TeamController.class);

    @Autowired
    private TeamService teamService;

    /**
     * list 车队信息列表
     *
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-team-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-list");

        int totals = teamService.count(login);
        if (totals > 0) {

            List<Team> teams = teamService.list(login, page.with(totals));

            modelAndView.addObject("teams", teams);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }

    /**
     * search 车队信息查询
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-team-management")
    public ModelAndView search(@Secure Login login) {
        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-search");
        return modelAndView;
    }

    /**
     * onSearch
     * 
     * @param login
     * @param teamSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-team-management")
    public ModelAndView onSearch(@Secure Login login, TeamSearch teamSearch, 
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-list");

        int totals = teamService.count(teamSearch, login);
        if (totals > 0) {

            List<Team> teams = teamService.search(teamSearch, login, page.with(totals));
            modelAndView.addObject("teams", teams);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));

        }

        return modelAndView;
        
    }
    
    /**
     * 查询车队是否存在
     * 
     * @param code
     * @return
     */
    @RequestMapping(value = "/exist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult exist(@RequestParam("code") String code) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            boolean exist = teamService.exist(code);
            httpResult.success().data("exist", exist)
                                .message("查询车队是否存在成功");
            
        } catch (Exception e) {
            logger.error("#exist() - get team by code fail, exception=[{}]", e);
            httpResult.fail("500").message("查询车队是否存在失败");
        }
        
        return httpResult;
        
    }

    /**
     * display create panel
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-team-management")
    public ModelAndView create(@Secure Login login) {
        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-create");
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param team
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(@Secure Login login, Team team) {

        HttpResult httpResult = new HttpResult();

        try {
            
            teamService.create(team, login);

            httpResult.success().message("新建成功");
            
        } catch (Exception e) {
            logger.error("#onCreate() - create team fail, exception is [{}]", e);
            httpResult.fail("500").message("新建失败");
        }
        
        return httpResult;
        
    }

    /**
     * display edit panel
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    @MenuSetting("submenu-team-management")
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-edit");

        modelAndView.addObject("team", teamService.load(id));

        return modelAndView;
        
    }

    /**
     * on edit
     * 
     * @param team
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{teamId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Preloading("teamId") Team team, 
                                           @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();

        try {
            
            teamService.edit(team, operator);

            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onEdit() - edit team fail, exception is [{}]", e);
            httpResult.fail("500").message("修改失败");
        }

        return httpResult;
        
    }

    /**
     * display delete panel
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    @MenuSetting("submenu-team-management")
    public ModelAndView delete(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/team/team-delete");

        modelAndView.addObject("id", id);

        return modelAndView;
        
    }

    /**
     * on delete
     * 
     * @param id
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDelete(@PathVariable("id") Long id, @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();

        try {
            
            teamService.disable(id, operator);

            httpResult.success().message("删除成功");
            
        } catch (Exception e) {
            logger.error("#onDelete() - delete team fail, exception is [{}]", e);
            httpResult.fail("500").message("删除失败");
        }

        return httpResult;
        
    }
    
}